/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coms.action;

import Util.Constant;
import coms.delegates.ConferenceRegistrationDelegate;
import coms.dto.ConferenceRegistration;
import model.InvoiceModel;
import coms.dto.User;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.jasperreports.engine.JasperCompileManager;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Pk
 */
public class InvoiceGeneratorAction extends ComsActionSupport {

    private InvoiceModel invoiceModel;
    private String conferenceBookingId;
    private ConferenceRegistrationDelegate conRegDel;
    private List<InvoiceModel> model;

    public List<InvoiceModel> getModel() {
        return model;
    }

    public void setModel(List<InvoiceModel> model) {
        this.model = model;
    }

    public InvoiceModel getInvoiceModel() {
        return invoiceModel;
    }

    public void setInvoiceModel(InvoiceModel invoiceModel) {
        this.invoiceModel = invoiceModel;
    }

    public String getConferenceBookingId() {
        return conferenceBookingId;
    }

    public void setConferenceBookingId(String conferenceBookingId) {
        this.conferenceBookingId = conferenceBookingId;
    }

    public InvoiceGeneratorAction() {
        conRegDel = new ConferenceRegistrationDelegate();
    }

    public String execute() {
        try {
            ConferenceRegistration conReg = conRegDel.getById(Integer.parseInt(conferenceBookingId));
            populateModel(conReg);
            String path = ServletActionContext.getServletContext().getRealPath("xml/invoice.jrxml");
            String path2 = ServletActionContext.getServletContext().getRealPath("xml/invoice.jasper");
            System.out.println(path);
            JasperCompileManager.compileReportToFile(path,path2);
        } catch (Exception e) {
            return ERROR;
        }
        return SUCCESS;
    }

    public void populateModel(ConferenceRegistration confReg) {
        User u = (User) getSession().get("user");
        System.out.println(u + "");
        invoiceModel = new InvoiceModel();
        invoiceModel.setName(u.getFullname());
        Date dt = new Date();
        SimpleDateFormat sdft = new SimpleDateFormat(Constant.DATETIME_FORMAT);
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_FORMAT);
        invoiceModel.setTodayDate(sdf.format(dt));
        invoiceModel.setBookingDate(sdf.format(confReg.getRegistrationDate()));
        invoiceModel.setConferenceCost("S$" + confReg.getConference().getCost() + ".00");
        invoiceModel.setConferenceDescription(confReg.getConference().getDetail());
        invoiceModel.setConferenceName(confReg.getConference().getName());
        invoiceModel.setConferenceStartDate(sdft.format(confReg.getConference().getConferenceStartDate()));
        invoiceModel.setConferenceLocation(confReg.getConference().getLocation().toString());
        //model=new ArrayList<InvoiceModel>();
        //model.add(invoiceModel);
    }
}
